(function(jQuery, undefined){
	
	fnCallback.fnAddFunction('fnArea', function(oData){
		window.oLocationData = oData;
	});
	
	var sHtmlTemplate = [
			"<div class=''>",
				"<p style='font-size:16px;text-indent:20px;color:#333;height:32px;line-height:32px;' class='g-yahei'>主要城市</p>",
				"<ul class='g-clear'>{{sRecommend}}</ul>",
				"<p style='font-size:16px;text-indent:20px;color:#333;height:32px;line-height:32px;' class='g-yahei'>其他省市</p>",
				"<ul class='g-clear'>{{sAll}}</ul>",
			"</div>"
		].join(''),
		sRecommendTpl = "<li id=\"{{id}}\" pid=\"{{pid}}\" name=\"{{name}}\" class='g-left location-recommend location-sub location-item' style='width:75px;height:40px;color:#666;text-align:center;line-height:40px;cursor:pointer;'>{{name}}</li>",
		sAllTpl = "<li id=\"{{id}}\" pid=\"{{pid}}\" name=\"{{name}}\" class='g-left location-all location-item' style='width:75px;height:40px;color:#666;text-align:center;line-height:40px;cursor:pointer;'>{{name}}</li>",
		jqProvinceDialog = null,
		jqCityDialog = null,
		oCurrentConfig = null,
		oCurrentProvince = null;
	
	function fnInit(jqThis, oConfig){
		if(jqProvinceDialog===null){
			var oAll = oLocationData.all,
				oRecommend = oLocationData.recommend;
				
			var aAll = [];
			$.each(oAll, function(key, val){
				aAll.push($.ff.substitute(sAllTpl, val));
			});
			
			var aRecommend = [];
			$.each(oRecommend, function(key, val){
				aRecommend.push($.ff.substitute(sRecommendTpl, val));
			});
				
			jqProvinceDialog = $($.ff.substitute(sHtmlTemplate, {
				sRecommend: aRecommend.join(''),
				sAll: aAll.join('')
			}));
		}	
	}
	
	function fnMain(jqThis, oConfig){
		jqThis.bind('click', function(){
			oCurrentConfig = oConfig;
			jqProvinceDialog.pDialog({
				title: '请选择',
				width: 700,
				height:350,
				hideDialogOnClose: true
			});	
		})
	}
	
	function fnTargetClick(obj){
		jqProvinceDialog && jqProvinceDialog.pDialog('close');
		jqCityDialog && jqCityDialog.pDialog('destroy');
		oCurrentConfig.fnOnChoose && oCurrentConfig.fnOnChoose.call(null, obj);
	}
	
	var aSubHtml = ["<div><ul class='g-clear'>"],
		sSubTpl = "<li id=\"{{id}}\" pid=\"{{pid}}\" name=\"{{name}}\" class='g-left location-sub location-item' style='width:150px;height:40px;color:#666;text-align:center;line-height:40px;cursor:pointer;'>{{name}}</li>";
	
	$('body').delegate('.location-item', 'mouseover', function(){
		$(this).css('backgroundColor', '#EEE');
	}).delegate('.location-item', 'mouseout', function(){
		$(this).css('backgroundColor', '#FFF');
	}).delegate('.location-all', 'click', function(){  //所有区域 2级地域点击
		var oSubObject = oLocationData.all[$(this).attr('id')];
		if(oSubObject.pid === '-1' || oSubObject._child === undefined){
			fnTargetClick(oSubObject);
			return false;	
		}
		oCurrentProvince = oSubObject;
		$.each(oSubObject._child, function(key, val){
			aSubHtml.push($.ff.substitute(sSubTpl, val));
		});
		aSubHtml.push("</ul><br/></div>");
		
		jqCityDialog = $(aSubHtml.join(""));
		
		jqCityDialog.pDialog({
			title: '请选择',
			width: 474
		});
		aSubHtml = ["<div><ul class='g-clear'>"];
		return false;
	}).delegate('.location-sub', 'click', function(){  
		var jqThis = $(this),
			sId = jqThis.attr('id'),
			obj = null;
		
		if(jqThis.hasClass('location-recommend') || jqThis.attr('pid') === '-1'){
			fnTargetClick(oLocationData.all[sId]);
			return;
		}else{
			fnTargetClick(oCurrentProvince._child[sId]);
			return;
		}
	});
	
	$.fn.pLocationDialog = function(oConfig){
		oConfig = $.extend(false, oDefaultConfig, oConfig);
		return this.each(function(){
			var jqThis = $(this);
			fnInit.call(jqThis, jqThis, oConfig);
			fnMain.call(jqThis, jqThis, oConfig);
		});
	}
	
	var oDefaultConfig = {};
})(jQuery);




